﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using Sinacor.Infra.Service.Security.Authorization;
using System.Data.Common;

namespace Sinacor.Servico.Bovespa.Ordens.ResourceAccess
{
    public class Motivo : BaseResourceAccess
    {
        #region Construtor

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public Motivo() :
            base(AuthorizationContext.Current.CompanyId)
        { }

        #endregion

        #region Métodos

        public void AlteraraMotivo(BusinessEntity.Motivo entity)
        {
            string sql = String.Empty;

            sql = @"UPDATE TORMOTIVO
                    SET DS_MOTIVO = :Descricao
                    WHERE CD_MOTIVO = :Codigo";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql))
            {
                this.DataBase.AddInParameter(command, "Descricao", System.Data.DbType.String, entity.Descricao);
                this.DataBase.AddInParameter(command, "Codigo", System.Data.DbType.Int32, entity.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        public void IncluirMotivo(BusinessEntity.Motivo entity)
        {
            string sql = String.Empty;

            sql = @"INSERT INTO TORMOTIVO (CD_MOTIVO,DS_MOTIVO) 
                    VALUES (:Codigo,:Descricao)";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql))
            {
                this.DataBase.AddInParameter(command, "Codigo", System.Data.DbType.Int32, entity.Codigo);
                this.DataBase.AddInParameter(command, "Descricao", System.Data.DbType.String, entity.Descricao);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        public void ExcluirMotivo(BusinessEntity.Parameter.FiltroMotivo parameter)
        {
            string sql = String.Empty;

            sql = @"DELETE FROM TORMOTIVO 
                    WHERE CD_MOTIVO = :Codigo";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql))
            {
                this.DataBase.AddInParameter(command, "Codigo", System.Data.DbType.Int32, parameter.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        public List<BusinessEntity.Motivo> ConsultarMotivos(BusinessEntity.Parameter.FiltroMotivo request)
        {
            List<BusinessEntity.Motivo> response;
            string sql = string.Empty;
            string where = string.Empty;
            string order = string.Empty;

            sql = @"SELECT 
                    CD_MOTIVO AS Codigo, 
                    DS_MOTIVO AS Descricao 
                    FROM TORMOTIVO 
                    where 1 = 1";

            order = @"ORDER BY CD_MOTIVO";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql))
            {
                if (request.Codigo != null)
                {
                    where = " and CD_MOTIVO = :Codigo ";
                    this.DataBase.AddInParameter(command, "Codigo", System.Data.DbType.Int32, request.Codigo);
                }

                if ((!String.IsNullOrEmpty(request.Descricao)) && (request.Descricao.Trim().Length > 0))
                {
                    where += " and DS_MOTIVO = :Descricao ";
                    this.DataBase.AddInParameter(command, "Descricao", System.Data.DbType.String, request.Descricao);
                }

                sql = String.Concat(sql, where, order);

                command.CommandText = sql;

                response = this.FindAll<BusinessEntity.Motivo>(command,
                    reader =>
                    {
                        BusinessEntity.Motivo item = new Sinacor.Servico.Bovespa.Ordens.BusinessEntity.Motivo();
                        item.Codigo = reader.GetValue<Int32>("Codigo");
                        item.Descricao = reader.GetValue<string>("Descricao");

                        return item;
                    });

                if (response != null)
                    return response;
                else
                    return new List<Sinacor.Servico.Bovespa.Ordens.BusinessEntity.Motivo>();
            }
 
        }
        #endregion 

    }
}
